﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using Microsoft.VisualBasic;
using System.Diagnostics;
using System.Collections;


public partial class UserControls_PageHeading : System.Web.UI.UserControl
{

    //public Label lblPageName
    //{ get { if (PageTitle1 == null) { return null; } else  return PageTitle1.PageName; } }

    //public Label lblPropertyName
    //{ get { if (PageTitle1 == null) { return null; } else return PageTitle1.PropertyName; } }

    //public Label lblPersonName
    //{ get { if (PageTitle1 == null) { return null; } else return PageTitle1.PersonName; } }

    //public event EventHandler Portfolios_SelectedIndexChanged;

    //public bool ShowOptions
    //{ get { return chkShowSettings.Checked; } }


    protected void Page_Load(object sender, EventArgs e)
    {

        //if (lblPageName != null)
        //{
        //    lblPageName.Text = "";
        //    lblPersonName.Text = "";
        //    lblPropertyName.Text = "";

        //    if (Request.QueryString.HasKeys())
        //    {
        //        if (Request.QueryString["rollup_type"] != null) lblPageName.Text = Request.QueryString["rollup_type"];
        //        if (Request.QueryString["rollup_id"] != null) lblPersonName.Text = Request.QueryString["rollup_id"];
        //    }
        //}
        string path = HttpUtility.UrlDecode(HttpContext.Current.Request.Url.PathAndQuery)
         .Replace("/ACC3_0/", "")
         .Replace(".aspx?1=", "\n")
         .Replace("&2=", "\n")
         .Replace("&3=", "\n");

        AddCurrentPathToHistory(path);

    }

    private void AddCurrentPathToHistory(string path)
    {

        Char[] DelimiterArray = new Char[1] { '\n' };
        string[] saPath = path.Split(DelimiterArray);
        ListItem lstItem = PathToListItem(path);
        Session["lstItem"] = lstItem;

        switch (saPath[0])
        {
            case "About":
            case "Home":
                break;

            default:
                ArrayList alHistoryLinks = (ArrayList)Session["alHistoryLinks"];
                if (alHistoryLinks == null) alHistoryLinks = new ArrayList();

                if (lstItem != null && !alHistoryLinks.Contains(lstItem))
                {

                    alHistoryLinks.Add(lstItem);
                    Session["alHistoryLinks"] = alHistoryLinks;
                }

                for (int i = alHistoryLinks.Count - 1; i >= 0; i--)
                {
                    ListItem li = (ListItem)alHistoryLinks[i];
                    blstHistory.Items.Add(li);
                }
                break;

        }

    }

    public ListItem PathToListItem(string path)
    {

        Debug.Print("\nPathToListItem\n======================================");

        Char[] DelimiterArray = new Char[1] { '\n' };
        string[] saPath = path.Split(DelimiterArray);
        Debug.Print("path=" + path);

        if (saPath.Length > 1)
        {
            string sPath = saPath[0];
            for (int i = 2; i < saPath.Length; i++)
            {
                sPath += "-" + saPath[i];
            }
            Debug.Print("sPath=" + sPath);

            ListItem lstItem = new ListItem(sPath, HttpUtility.UrlDecode(HttpContext.Current.Request.Url.AbsoluteUri));

            return lstItem;

        }
        else
            return null;

    }

    public static void AddUserHistoryToList(ref BulletedList blstHistory)
    {

        Debug.Print("\nAddUserHistoryToList\n======================================");
        Debug.Print("blstHistory.Count=" + blstHistory.Items.Count);
        //get the user's History into menuitems from collection in cookie:      


        HttpCookie cookieHistory = HttpContext.Current.Request.Cookies["ACC3_0.History"];

        if (cookieHistory != null && cookieHistory.HasKeys)
        {
            Debug.Print("cookieHistory.Values.Count=" + cookieHistory.Values.Count);
            for (int i = cookieHistory.Values.Count - 1; i >= 0; i--)
            {
                ListItem mnuItem = new ListItem(cookieHistory.Values.AllKeys[i], cookieHistory.Values[i]);
                Debug.Print(string.Format("cookieHistory.Values[{0}] :=: {1}", i, mnuItem.Text + "=" + mnuItem.Value));

                Int32 nInt;
                if (blstHistory.Items.FindByText(mnuItem.Text) == null
                 && !Int32.TryParse(mnuItem.Text, out nInt))
                {
                    blstHistory.Items.Add(mnuItem);
                    Debug.Print("Added to blstHistory.Count=" + blstHistory.Items.Count);
                }
            }

        }
        else
        {
            Debug.Print("cookieHistory == null");
        }
    }



    //private void AddPathToCookies(string path)
    //{
    //    Char[] DelimiterArray = new Char[1] { '\n' };
    //    string[] saPath = path.Split(DelimiterArray);

    //    if (saPath.Length > 1)
    //    {
    //        //save page URL to Cookie
    //        HttpCookie cookieHistory = Request.Cookies.Get("ACC3_0.History");
    //        if (cookieHistory == null)
    //        {
    //            cookieHistory = new HttpCookie("ACC3_0.History");
    //            cookieHistory.Expires.AddMonths(1);
    //        }

    //        string sPath = saPath[0];
    //        for (int i = 2; i < saPath.Length; i++)
    //        {
    //            sPath += "-" + saPath[i];
    //        }
    //        if (!cookieHistory.Values.AllKeys.Contains(sPath))
    //        {
    //            cookieHistory.Values.Add(sPath, HttpContext.Current.Request.Url.AbsoluteUri);
    //            Response.Cookies.Add(cookieHistory);
    //        }
    //    }
    //}

    //public void AddUserHistoryToList()
    //{
    //    //get the user's History into menuitems from collection in cookie:      
    //    HttpCookie cookieHistory = Request.Cookies["ACC3_0.History"];
    //    if (cookieHistory != null && cookieHistory.HasKeys)
    //    {
    //        for (int i = cookieHistory.Values.Count - 1; i >= 0; i--)
    //        {
    //            ListItem mnuItem = new ListItem(cookieHistory.Values.AllKeys[i], cookieHistory.Values[i]);

    //            Int32 nInt;
    //            if (blstHistory.Items.FindByText(mnuItem.Text) == null
    //             && !Int32.TryParse(mnuItem.Text, out nInt))
    //                blstHistory.Items.Add(mnuItem);
    //        }

    //    }
    //}


}

